//
//  SelfViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/8/29.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "SelfViewController.h"
#import "SelfTableViewCell.h"
#import "TAMacro.h"
#import "StarredChannelsViewController.h"
#import "SelfNameTableViewCell.h"
#import "OptionsViewController.h"
#import "AddPersonViewController.h"
#import "SearchPersonContent.h"
#import "UIImageView+WebCache.h"
@interface SelfViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic, strong) SearchPersonContent *mySelf;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *selfTableView;

@end

@implementation SelfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createTableView];
    [self createNavigationBar];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *user = [userDefault objectForKey:@"user"];
    self.mySelf = [SearchPersonContent modelWithDic:user[@"content"]];
    [self.selfTableView reloadData];
    
}

- (void)createTableView {
    
    self.selfTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    _selfTableView.backgroundColor = [UIColor whiteColor];
    _selfTableView.delegate = self;
    _selfTableView.dataSource = self;
    [self.view addSubview:_selfTableView];
    
    [_selfTableView registerClass:[SelfTableViewCell class] forCellReuseIdentifier:@"selfCellReuse"];
    [_selfTableView registerNib:[UINib nibWithNibName:@"SelfTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SelfTableViewCellReuse"];
    [_selfTableView registerNib:[UINib nibWithNibName:@"SelfNameTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SelfNameTableViewCellReuse"];    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    }
    return 50;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        SelfNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelfNameTableViewCellReuse"];
        cell.nameLabel.text = _mySelf.uname;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _mySelf.avatar]]];
        if ([_mySelf.sex isEqualToString:@"1"]) {
            cell.genderImage.image = [UIImage imageNamed:@"sexMale"];
        } else {
            cell.genderImage.image = [UIImage imageNamed:@"sexFemale"];
        }
        
        return cell;
    }
    SelfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelfTableViewCellReuse"];
    if (indexPath.row == 1) {
        cell.nameLabel.text = @"我关注的版块";
        cell.headerImage.image = [UIImage imageNamed:@"Y"];
    } else if (indexPath.row == 2) {
        cell.nameLabel.text = @"我关注的人";
        cell.headerImage.image = [UIImage imageNamed:@"knowUserAttentioned"];
    } else if (indexPath.row == 3) {
        cell.nameLabel.text = @"设置";
        //settings_menu
        cell.headerImage.image = [UIImage imageNamed:@"settings_menu"];

    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 1) {
        StarredChannelsViewController *allChannelsVC = [[StarredChannelsViewController alloc] init];
        [self.navigationController pushViewController:allChannelsVC animated:YES];
    }
    if (indexPath.row == 3) {
        OptionsViewController *optionsVC = [[OptionsViewController alloc] init];
        [self.navigationController pushViewController: optionsVC animated:YES];
    }
}


- (void)createNavigationBar {
    
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textAlignment = 1;
    _titleLabel.textColor = [UIColor whiteColor];
    [self.backView addSubview:_titleLabel];
    _titleLabel.text = @"我";

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH - 40, 27, 30, 30);
    [button setImage:[UIImage imageNamed:@"add_user-48"] forState:UIControlStateNormal];
    [self.backView addSubview:button];
    
    [button addTarget:self action:@selector(addPerson:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)addPerson:(UIButton *)button {
    AddPersonViewController *addPersonVC = [[AddPersonViewController alloc] init];
    [self.navigationController pushViewController:addPersonVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
